/**
 *  JFTSTool - Java Full Text Search tool.
 *  Copyright (C) <2009>  <grupo JFTS>
 *  This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jftstool.fileParserFramework.pdfHandler;

import java.io.IOException;

import org.apache.commons.io.IOExceptionWithCause;
import org.jftstool.fileParserFramework.contentHandler.XHTMLResultHandler;
import org.jftstool.fileParserFramework.exceptions.DocumentHandlerException;
import org.jftstool.fileParserFramework.metadata.DocumentMetaData;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDPage;
import org.pdfbox.util.PDFTextStripper;
import org.pdfbox.util.TextPosition;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

/**
 * @author grupo JFTS
 *
 */
public class PDFToResultHandler extends PDFTextStripper {
	private final XHTMLResultHandler handler;
	
	private PDFToResultHandler(ContentHandler resultHandler, DocumentMetaData metaData) throws IOException {
		this.handler = new XHTMLResultHandler(resultHandler, metaData);
	}

	
	public static void pdfToText(PDDocument document, ContentHandler resultHandler, DocumentMetaData metaData) throws DocumentHandlerException {
		try {
			PDFToResultHandler result = new PDFToResultHandler(resultHandler, metaData);
			result.getText(document);
		}
		catch (IOException e) {
			if (e.getCause() instanceof SAXException){
				throw new DocumentHandlerException("Error building resultHandler"); 
			}
			else{
				throw new DocumentHandlerException("Error, can't convert PDF to resultHandler");				
			}
		}
	}
	
	
	protected void endDocument(PDDocument pdf)throws IOException {
		try {
			handler.endDocument();
		}
		catch (SAXException e) {
			throw new IOExceptionWithCause("Error, can't end document", e);			
		}
	}
	
	protected void endPage(PDPage page) throws IOException {
		try {
			handler.endElement("div");
		}
		catch (SAXException e) {
			throw new IOExceptionWithCause("Error, can't end page", e);			
		}
	}
	
	protected void endParagraph() throws IOException {
		try {
			handler.endElement("p");
		}
		catch (SAXException e) {
			throw new IOExceptionWithCause("Error, can't end paragraph", e);			
		}
	}
	
	protected void startDocument(PDDocument pdf) throws IOException {
		try {
			handler.startDocument();
		}
		catch (SAXException e) {
			throw new IOExceptionWithCause("Error, can't start document", e);			
		}
	}
	
	protected void startParagraph() throws IOException {
		try {
			handler.startElement("p");
		}
		catch (SAXException e) {
			throw new IOExceptionWithCause("Error, can't start paragraph", e);			
		}
	}
	
	protected void startPage(PDPage page) throws IOException {
		try {
			handler.startElement("div");
		}
		catch (SAXException e) {
			throw new IOExceptionWithCause("Error, can't start page", e);			
		}
	}
	
	 protected void writeCharacters(TextPosition text) throws IOException {
		 try {
			 handler.characters(text.getCharacter());
	     }
		 catch (SAXException e) {
			 throw new IOExceptionWithCause("Error to write a character: " + text.getCharacter(), e);
	    }
	 }
	
	 
	 public String getLineSeparator() {
		 try {
			 handler.characters("\n");
	     }
		 catch(SAXException e) { /*do nothing */ }
        
		 return super.getLineSeparator();
	 }
}
